import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      name: 'home',
      component: (resolve) => require(['@/components/Home'], resolve),
      redirect: '/index',
      children: [
        {
          path: '/index',
          name: 'index',
          component: (resolve) => require(['@/components/Index'], resolve),
          meta: { title: '后台主页' },
        },
        {
          path: '/menus/list',
          name: 'menus-list',
          component: (resolve) => require(['@/components/menu/MenuList'], resolve),
          meta: { title: '菜单项列表' },
        },
        {
          path: '/rota/img/list',
          name: 'rota-img-list',
          component: (resolve) => require(['@/components/adv/IndexRotationImg'], resolve),
          meta: { title: '首页轮播图' },
        },
        {
          path: '/news/list',
          name: 'news-list',
          component: (resolve) => require(['@/components/news/NewsList'], resolve),
          meta: { title: '新闻列表' },
        },
      ]
    }
  ]
})

//挂载路由导航守卫
router.beforeEach((to, from, next) => {
  //to:将要访问的路径 from: 从哪个路径来 next：一个函数，表示放行 next()--放行， next('/login')--强制跳转
  if (to.meta.title) {
    document.title = to.meta.title
  }
  next()
})

export default router